package com.zyw.zhuangdian.listener;

import com.zyw.zhuangdian.event.SystemMessageSendEvent;
import com.zyw.zhuangdian.service.MessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Order(1)
@RequiredArgsConstructor
public class SendMessageListener implements ApplicationListener<SystemMessageSendEvent> {

    private final MessageService messageService;

    @Override
    public void onApplicationEvent(SystemMessageSendEvent systemMessageSendEvent) {
        log.info("接收事件 记录消息 {}",systemMessageSendEvent.getMessageDtoIn());
        if (systemMessageSendEvent.getMessageDtoIn()!=null){
            messageService.addMessage(systemMessageSendEvent.getMessageDtoIn());
        }else{
            throw new IllegalArgumentException("发送消息不能为空！");
        }
    }
}
